﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace EditUsers_Better.ManageUsersModule
{
    public class MemoryUserRepository : IUserRepository
    {
        public Dictionary<string, User> Entities { get; private set; }

        public MemoryUserRepository()
        {
            Entities = new Dictionary<string, User>();
        }

        public string Create(User entity)
        {
            entity.Id = Guid.NewGuid().ToString();
            Entities[entity.Id] = entity;
            return entity.Id;
        }

        public bool Update(User entity)
        {
            if (!Entities.ContainsKey(entity.Id))
                return false;

            Entities[entity.Id] = entity;
            return true;
        }

        public bool Delete(User entity)
        {
            return Delete(entity.Id);
        }

        public bool Delete(string entityId)
        {
            return Entities.Remove(entityId);
        }

        public User Get(string entityId)
        {
            User entity = default(User);
            Entities.TryGetValue(entityId, out entity);
            return entity;
        }

        public IEnumerable<User> GetAll()
        {
            return Entities.Values;
        }

        public IEnumerable<User> FindByName(string criteria)
        {
            return from c in Entities.Values
                   where
                      c.FirstName.IndexOf(criteria, StringComparison.CurrentCultureIgnoreCase) != -1 ||
                      c.LastName.IndexOf(criteria, StringComparison.CurrentCultureIgnoreCase) != -1
                   select c;
        }
    }
}
